#include "devchecker.h"
#include "controller.h"
#include "log4z.h"
#include <pthread.h>

bool bBlankScreen = false;
bool bPowerMonitor = true;
//Check the SMT32 device
void* chkSMT32(void* ptr)
{
    int* idata = (int*)ptr;
    if (idata != 0)
    {
        Controller ettaCtrl;
        *idata = ettaCtrl.CheckLight();
        LOGFMTD("Func %s SMT32 return=%d", __func__, *idata);
    }
    return ((void *)0);
}

//Check RuDong device
void* chkRuDong(void* ptr)
{
    int* idata = (int*)ptr;
    if (idata != 0)
    {
        Controller ettaCtrl;
        *idata = ettaCtrl.StopRuDong();
        LOGFMTD("Func %s RuDong return=%d", __func__, *idata);
    }
    return ((void *)0);
}

//Check motor device
void* chkMotor(void* ptr)
{
    int* idata = (int*)ptr;
    if (idata != 0)
    {
        Controller ettaCtrl;
        int iSt = ettaCtrl.StartIo();
        int iTot = 0;
        if (iSt == 1)
        {
            //guiwei
            ettaCtrl.SpeedMotor(1);
            ettaCtrl.StartMotor();
            while (iSt == 1 && iTot++ <= 50)
            {
                QThread::msleep(200);
                iSt = ettaCtrl.StartIo();
            }
            ettaCtrl.StopMotor();
        }
        //Timeout
        if (iTot > 50)
        {
            *idata = -1;
        }
        else
        {
            *idata = 1;
        }
        LOGFMTD("Func %s Motor return=%d", __func__, *idata);
    }
    return ((void *)0);
}

//Reset yaodong
void* chkYaodong(void* ptr)
{
    int* idata = (int*)ptr;
    if (idata != 0)
    {
        Controller ettaCtrl;
        *idata = ettaCtrl.CloseYao();
        LOGFMTD("Func %s YaoDong return=%d", __func__, *idata);
    }
    return ((void *)0);
}

//Open the door
void* chkDoor(void* ptr)
{
    int* idata = (int*)ptr;
    if (idata != 0)
    {
        Controller ettaCtrl;
        *idata = ettaCtrl.OpenDoor();
        LOGFMTD("Func %s Door return=%d", __func__, *idata);
    }
    return ((void *)0);
}

void* chkPower(void* ptr)
{
    Controller ettaCtrl;
    unsigned int iPowerShutdown = 0, iPowerSuspend = 0, iPowerLow = 0;
//    bool bLightBlink = false;
//    bool bResumeLight = false;
    bool bSetLight = false;
    while (bPowerMonitor)
    {
        if (ettaCtrl.PowerIo() == 0)
        {
            iPowerShutdown++;
            iPowerSuspend = 1;
            iPowerLow = 0;
        }
        else
        {
            iPowerShutdown = 0;
            iPowerLow = 1;
        }
        //Keep for 6s
        if (iPowerShutdown >= 30)
        {
            //reset value
            iPowerShutdown = 0;
            iPowerSuspend = 0;
            iPowerLow == 0;
            LOGFMTD("Func %s: Power key press more t han 6 seconds.", __func__);
            system("echo 1 > /sys/class/graphics/fb0/blank");
            DevChecker* dc = (DevChecker*)(ptr);
            if (0 != dc)
            {
                emit dc->progressChange(8008);
            }
        }
        //Keep for 2s
        if (iPowerSuspend == 1 && iPowerLow == 1)
        {
            //reset value
            iPowerShutdown = 0;
            iPowerSuspend = 0;
            iPowerLow == 0;
            LOGFMTD("Func %s: Power key suspend.", __func__);
            if (bBlankScreen)
            {
                bBlankScreen = !bBlankScreen;
                //unblank
                system("echo 0 > /sys/class/graphics/fb0/blank");
                bSetLight = true;
            }
            else
            {
                bBlankScreen = !bBlankScreen;
                //blank
                system("echo 1 > /sys/class/graphics/fb0/blank");
                bSetLight = true;
            }
        }
        //blue light
        if (bBlankScreen)
        {
//            bLightBlink = !bLightBlink;
//            if (bLightBlink)
//            {
//                system("echo 1 > /sys/class/gpio/gpio112/value");
//            }
//            else
//            {
//                system("echo 0 > /sys/class/gpio/gpio112/value");
//            }
//            bResumeLight = true;
            if (bSetLight)
            {
                system("echo 0 > /sys/class/gpio/gpio112/value");
                system("echo 1 > /sys/class/gpio/gpio60/value");
                bSetLight = false;
            }
        }
        else
        {
            //only one time
//            if (bResumeLight)
//            {
//                bResumeLight = false;
//                system("echo 1 > /sys/class/gpio/gpio112/value");
//            }
            if (bSetLight)
            {
                system("echo 0 > /sys/class/gpio/gpio60/value");
                system("echo 1 > /sys/class/gpio/gpio112/value");
                bSetLight = false;
            }
        }
        QThread::msleep(200);
    }
    LOGD("Power monitor thread exit!");
    return ((void *)0);
}

DevChecker::DevChecker(QObject *parent):QThread(parent)
{

}

DevChecker::~DevChecker()
{

}

//Thread running function
void DevChecker::run()
{
    LOGFMTD("Func %s Start Light check", __func__);
    //start one thread to check Light
    pthread_t smt32_tid;
    int iSMT32 = 256;
    int err = pthread_create(&smt32_tid, NULL, chkSMT32, &iSMT32);
    if(err != 0){
        //error
    }

    for(int i=0; i<20; i++)
    {
        //check SMT32 finish
        if (iSMT32 != 256)
        {
            if (iSMT32 > 0)
            {
                emit progressChange(20);
                break;
            }
            else
            {
                if (iSMT32 == -1)
                {
                    emit progressChange(8001);
                }
                else if (iSMT32 == -2)
                {
                    emit progressChange(8006);
                }
                else if (iSMT32 == -3)
                {
                    emit progressChange(8007);
                }
                return;
            }
        }
        else
        {
            emit progressChange(i);
            QThread::msleep(100);
        }
    }

    pthread_join(smt32_tid, 0);
    if (iSMT32 <=0 || iSMT32 == 256)
    {
        if (iSMT32 == -1)
        {
            emit progressChange(8001);
        }
        else if (iSMT32 == -2)
        {
            emit progressChange(8006);
        }
        else if (iSMT32 == -3)
        {
            emit progressChange(8007);
        }
        return;
    }
    LOGFMTD("Func %s Start Rudong check", __func__);
    //Start one thread to check RuDong
    pthread_t rudong_tid;
    int iDong = 256;
    err = pthread_create(&rudong_tid, NULL, chkRuDong, &iDong);
    if(err != 0){
        //error
    }
    for(int i=21; i<40; i++)
    {
        //check Rudong finish
        if (iDong != 256)
        {
            if (iDong > 0)
            {
                emit progressChange(40);
                break;
            }
            else
            {
                emit progressChange(8002);
                return;
            }
        }
        else
        {
            emit progressChange(i);
            QThread::msleep(100);
        }
    }

    pthread_join(rudong_tid, 0);
    if (iDong <=0 || iDong == 256)
    {
        emit progressChange(8002);
        return;
    }
    LOGFMTD("Func %s Start GuangDian check", __func__);
    //Start one thread to check GuangDian
    pthread_t guangdian_tid;
    int iGuang = 256;
    err = pthread_create(&guangdian_tid, NULL, chkMotor, &iGuang);
    if(err != 0){
        //error
    }
    for(int i=41; i<60; i++)
    {
        //check Motor finish
        if (iGuang != 256)
        {
            if (iGuang > 0)
            {
                emit progressChange(60);
                break;
            }
            else
            {
                emit progressChange(8003);
                return;
            }
        }
        else
        {
            emit progressChange(i);
            QThread::msleep(800);
        }
    }

    pthread_join(guangdian_tid, 0);
    if (iGuang <=0 || iGuang == 256)
    {
        emit progressChange(8003);
        return;
    }
    LOGFMTD("Func %s Start Yaodong check", __func__);
    //Start one thread to check Yaodong
    pthread_t yaodong_tid;
    int iYao = 256;
    err = pthread_create(&yaodong_tid, NULL, chkYaodong, &iYao);
    if(err != 0){
        //error
    }
    for(int i=61; i<80; i++)
    {
        //check Yaodong finish
        if (iYao != 256)
        {
            if (iYao > 0)
            {
                emit progressChange(80);
                break;
            }
            else
            {
                emit progressChange(8004);
                return;
            }
        }
        else
        {
            emit progressChange(i);
            QThread::msleep(100);
        }
    }

    pthread_join(yaodong_tid, 0);
    if (iYao <=0 || iYao == 256)
    {
        emit progressChange(8004);
        return;
    }
    LOGFMTD("Func %s Start Door check", __func__);
    //Start one thread to check Door
    pthread_t door_tid;
    int iDoor = 256;
    err = pthread_create(&door_tid, NULL, chkDoor, &iDoor);
    if(err != 0){
        //error
    }
    for(int i=81; i<100; i++)
    {
        //check Door open finish
        if (iDoor != 256)
        {
            if (iDoor > 0)
            {
                emit progressChange(100);
                break;
            }
            else
            {
                emit progressChange(8005);
                return;
            }
        }
        else
        {
            emit progressChange(i);
            QThread::msleep(800);
        }
    }

    pthread_join(door_tid, 0);
    if (iDoor > 0 && iDoor != 256)
    {
        emit progressChange(100);
    }
    else
    {
        emit progressChange(8005);
    }
    LOGFMTD("Door result %d", iDoor);
    //one thread to monitor power
    pthread_t power_tid;
    err = pthread_create(&power_tid, NULL, chkPower, this);
}
